package com.example.pkmedical.Activity.servicecode;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.ToastUtils;

import java.lang.reflect.Method;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InputCodeActivity extends BaseActivity {

    @BindView(R.id.top_view)
    View topView;
    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.top_icon_layout)
    RelativeLayout topIconLayout;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;
    @BindView(R.id.main_date)
    TextClock mainDate;
    @BindView(R.id.main_topbar)
    RelativeLayout mainTopbar;
    @BindView(R.id.inputcode_edit)
    EditText inputcodeEdit;
    @BindView(R.id.input_one)
    RelativeLayout inputOne;
    @BindView(R.id.input_two)
    RelativeLayout inputTwo;
    @BindView(R.id.input_three)
    RelativeLayout inputThree;
    @BindView(R.id.input_four)
    RelativeLayout inputFour;
    @BindView(R.id.input_five)
    RelativeLayout inputFive;
    @BindView(R.id.input_six)
    RelativeLayout inputSix;
    @BindView(R.id.input_seven)
    RelativeLayout inputSeven;
    @BindView(R.id.input_eight)
    RelativeLayout inputEight;
    @BindView(R.id.input_nine)
    RelativeLayout inputNine;
    @BindView(R.id.input_last)
    RelativeLayout inputLast;
    @BindView(R.id.input_zero)
    RelativeLayout inputZero;
    @BindView(R.id.input_goback)
    RelativeLayout inputGoback;
    @BindView(R.id.key_bord)
    LinearLayout keyBord;
    @BindView(R.id.bt_input_yes)
    Button btInputYes;
    @BindView(R.id.bt_input_no)
    Button btInputNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inputcode);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_sz);
        mainHouseText.setText(R.string.setting_service_code_bt);
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            inputcodeEdit.setInputType(InputType.TYPE_NULL);
        } else {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setSoftInputShownOnFocus;
                setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setSoftInputShownOnFocus.setAccessible(true);
                setSoftInputShownOnFocus.invoke(inputcodeEdit, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.input_one, R.id.input_two, R.id.input_three, R.id.input_four, R.id.input_five, R.id.input_six, R.id.input_seven, R.id.input_eight, R.id.input_nine, R.id.input_last, R.id.input_zero, R.id.input_goback, R.id.bt_input_yes, R.id.bt_input_no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.input_one:
                inputcodeEdit.setText(inputcodeEdit.getText()+"1");
                break;
            case R.id.input_two:
                inputcodeEdit.setText(inputcodeEdit.getText()+"2");
                break;
            case R.id.input_three:
                inputcodeEdit.setText(inputcodeEdit.getText()+"3");
                break;
            case R.id.input_four:
                inputcodeEdit.setText(inputcodeEdit.getText()+"4");
                break;
            case R.id.input_five:
                inputcodeEdit.setText(inputcodeEdit.getText()+"5");
                break;
            case R.id.input_six:
                inputcodeEdit.setText(inputcodeEdit.getText()+"6");
                break;
            case R.id.input_seven:
                inputcodeEdit.setText(inputcodeEdit.getText()+"7");
                break;
            case R.id.input_eight:
                inputcodeEdit.setText(inputcodeEdit.getText()+"8");
                break;
            case R.id.input_nine:
                inputcodeEdit.setText(inputcodeEdit.getText()+"9");
                break;
            case R.id.input_last:
                if (inputcodeEdit.getText().toString().length()>0){
                    inputcodeEdit.setText(inputcodeEdit.getText().toString().substring(0, inputcodeEdit.getText().toString().length()-1));
                }

                break;
            case R.id.input_zero:
                inputcodeEdit.setText(inputcodeEdit.getText()+"0");
                break;
            case R.id.input_goback:
                inputcodeEdit.setText("");
                break;
            case R.id.bt_input_yes:
            if (!TextUtils.isEmpty(inputcodeEdit.getText().toString()) && CommandUtil.PASSWORD.equals(inputcodeEdit.getText().toString())){
                startActivity(new Intent(MyApp.getAppContext(), InputCodeNextActivity.class));
                InputCodeActivity.this.finish();
//                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }else {
                ToastUtils.show(this, getResources().getString(R.string.pswerror));
            }
                break;
            case R.id.bt_input_no:
                InputCodeActivity.this.finish();
                break;
        }
    }
}
